package model.semantics;

import model.State;
import model.parser.ParserException;

/**
 * Represents an assignment expression (=).
 * @author Sterling Dorminey
 */
public class AssignmentNode<T> extends Node<T> {
	private String myLabel;
	private Node<T> myChild;
	
	/**
	 * Takes a left node, whose label is to be used as the key,
	 * and a right node, which is the reference to which the key will point.
	 */
	public AssignmentNode(Node<T> labelNode, Node<T> child) {
		// Can only construct an assignment if the left side is a variable node.
		if(!(labelNode instanceof VariableNode)) {
			throw ParserException.BAD_ARGUMENTS;
		}
		
		myLabel = labelNode.toString();
		myChild = child;
	}
	
	@Override
	public T evaluate(State<T> state) {
		state.setAssignment(myLabel, myChild);
		return myChild.evaluate(state);
	}

	@Override
	public String toString() {
		return "(=" + myLabel + " " + myChild.toString()+ ")";
	}
}
